layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#createTimeStart'
        ,value:new Date()
    });

    laydate.render({
        elem: '#createTimeEnd'
        ,value:new Date()
    });

    /**
     * 齐河小车任务情况
     */
    var BiQhAgvTask = {
        tableId: "biQhAgvTaskTable"
    };

    /**
     * 齐河AGV任务情况
     */
    BiQhAgvTask.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'srcPositionCode', sort: true, title: '起始位置', style:'font-weight: 600;'},
            {field: 'tarPositionCode', sort: true, title: '终点位置', style:'font-weight: 600;'},
            {field: 'priority', sort: true, title: '优先级', style:'font-weight: 600;'},
            {field: 'taskNo', sort: true, title: '任务编号', style:'font-weight: 600;'},
            {field: 'agvCode', sort: true, title: 'AGV编码', style:'font-weight: 600;'},
            {field: 'statShow', sort: true, title: '任务状态', style:'font-weight: 600;'},
            {field: 'createUserName', sort: true, title: '创建用户', style:'font-weight: 600;'},
            {field: 'createTime', sort: true, title: '创建时间', style:'font-weight: 600;'},
            {field: 'getCompleteTime', sort: true, title: '取货时间', style:'font-weight: 600;'},
            {field: 'putCompleteTime', sort: true, title: '放货时间', style:'font-weight: 600;'},
            {field: 'cancelTime', sort: true, title: '取消时间', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BiQhAgvTask.search = function () {
        var queryData = {};
        queryData['stat'] = $("#stat").val();
        queryData['createTimeStart'] = $("#createTimeStart").val();
        queryData['createTimeEnd'] = $("#createTimeEnd").val();
        queryData['srcPositionCode'] = $("#srcPositionCode").val();
        queryData['tarPositionCode'] = $("#tarPositionCode").val();
        table.reload(BiQhAgvTask.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 // $('#btnSearch').click(function () {
                 //     BiQhAgvTask.search();
                 // });
                 // // 导出excel
                 // $('#btnExp').click(function () {
                 //     BiQhAgvTask.exportExcel();
                 // });
            }
        });
    };

    /**
     * 导出excel按钮
     */
    BiQhAgvTask.exportExcel = function () {
        var checkRows = table.checkStatus(BiQhAgvTask.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['stat'] = $("#stat").val();
            queryData['createTimeStart'] = $("#createTimeStart").val();
            queryData['createTimeEnd'] = $("#createTimeEnd").val();
            queryData['srcPositionCode'] = $("#srcPositionCode").val();
            queryData['tarPositionCode'] = $("#tarPositionCode").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/biQhAgvTask/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BiQhAgvTask.tableId,
        url: Feng.ctxPath + '/biQhAgvTask/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BiQhAgvTask.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BiQhAgvTask.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BiQhAgvTask.exportExcel();
    });

});
